import interfaces.Detect;

import java.util.Calendar;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import models.DS_DomainModel;
import models.JavaCommandLineServiceAccounts;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.reflections.Reflections;

import util.ConnectionHandle;
import util.S3QueueHandle;
import util.Utils;
import entities.Domain;

public class Demo extends Thread {

	@Override
	public void run() {
		while (true) {
			Reflections reflections = new Reflections("detect");
			Set<Class<? extends Detect>> classes = reflections
					.getSubTypesOf(Detect.class);

			Map<String, Object> obj = new TreeMap<String, Object>();
			String domainName = S3QueueHandle.pullQueue();
			if (domainName != null && domainName.length() > 0
					&& Utils.isValidDomain(domainName)) {
				System.out.println("Processing domain : " + domainName);
				System.out.println("----------------------------------");
				if (!DS_DomainModel.isExistDomain(domainName)) {
					Domain domain = ConnectionHandle
							.fetchDomainByUrlThroughJsoup("http://"
									+ domainName);
					boolean success = false;
					if (domain != null) {
						Document doc = Jsoup.parse(domain.getHtml());
						obj.put("domain", domain);
						obj.put("doc", doc);

						for (Class<? extends Detect> class1 : classes) {
							try {
								class1.newInstance().getInfo(obj);
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
						domain.setLastUpdate(Calendar.getInstance().getTime());
						DS_DomainModel.insertDatastore(domain);
						JavaCommandLineServiceAccounts.insertDomain(domain);
						success = true;
					} else {
						success = false;
					}
					if (!success) {
						S3QueueHandle.putErrorQueue(domainName);
						System.out.println("Invalid domain !");
					}
				} else {
					System.out.println("Domain exist !");
				}
				System.out.println("----------------------------------");
			} else {
				System.out.println("Have no domain ! ");
				try {
					sleep(5000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static void main(String[] args) {
		System.out.println("Start process...");
		// Demo d = new Demo();
		// d.start();
		try {
			ExecutorService executorService = Executors.newFixedThreadPool(500);
			for (int i = 0; i < 1000; i++) {
				executorService.execute(new Demo());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		// test("linkhay.com");

		// String domainName = S3QueueHandle.pullQueue();
		// test(domainName);
	}

	public static void test(String domainName) {
		System.out.println("Start Process - " + domainName);
		Reflections reflections = new Reflections("detect");
		Set<Class<? extends Detect>> classes = reflections
				.getSubTypesOf(Detect.class);

		Map<String, Object> obj = new TreeMap<String, Object>();
		Domain domain = ConnectionHandle.fetchDomainByUrlThroughJsoup("http://"
				+ domainName);
		if (domain != null) {
			Document doc = Jsoup.parse(domain.getHtml());
			obj.put("domain", domain);
			obj.put("doc", doc);
			for (Class<? extends Detect> class1 : classes) {
				try {
					class1.newInstance().getInfo(obj);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			domain.setLastUpdate(Calendar.getInstance().getTime());
			DS_DomainModel.insertDatastore(domain);
			JavaCommandLineServiceAccounts.insertDomain(domain);
		} else {
			S3QueueHandle.putErrorQueue(domainName);
			System.out.println("Invalid domain !");
		}
	}

}
